const fs = require('fs');
const path = require('path');
const action = require('./action');
const api = require('./api');
const component = require('./component');
const const1 = require('./const');
const reducer = require('./reducer');
const route = require('./route');
const index = require('./index');

const name = process.argv[2];
const text = process.argv[3];
const folder = process.argv[4];
(function (name, text, folder) {
  if (typeof name != 'string') {
    throw new Error('Please Enter Component Name');
  }
  if (!(/^\w+$/).test(name)) {
    console.log(` Controller name (${name}) valid`.red);
    return;
  }
  const pagePath = path.resolve(__dirname, '../src/pages' + (folder || '') + '/' + name);
  if (!fs.existsSync(pagePath)) {
    fs.mkdirSync(pagePath);
  }
  // create action
  action(pagePath, name, text);
  // create api
  api(pagePath, name);
  // create component
  component(pagePath, name, text);
  // create const
  const1(pagePath, name);
  // create reducer
  reducer(pagePath, name);
  // create route
  route(pagePath, name);
  // create index.js
  index(pagePath, name);

})(name, text, folder)
